package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallAdmin.Column;
import org.linlinjava.litemall.db.mapper.LitemallAdminDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallAdminService {
    private final Column[] result = new Column[]{Column.id, Column.username, Column.avatar, Column.roleIds};
    @Resource
    private LitemallAdminDao adminDao;

    public List<LitemallAdmin> findAdmin(String username) {
    	
    	
    	LitemallAdmin admin = new LitemallAdmin();
    	admin.setUsername(username);
    	return adminDao.template(admin);
    	
		/*
		 * LitemallAdminExample example = new LitemallAdminExample();
		 * example.or().andUsernameEqualTo(username).andDeletedEqualTo(false); return
		 * adminMapper.selectByExample(example);
		 */
    }

    public LitemallAdmin findAdmin(Integer id) {
    	return adminDao.single(id);
		/* return adminMapper.selectByPrimaryKey(id); */
    }

    public List<LitemallAdmin> querySelective(String username, Integer page, Integer limit, String sort, String order) {
    	
    	
    	LambdaQuery<LitemallAdmin> query = adminDao.createLambdaQuery();
    	if (!StringUtils.isEmpty(username)) {
    		query.andLike(LitemallAdmin::getUsername ,  "%" + username + "%");
        }
    	query.andEq(LitemallAdmin::getDeleted, false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        
        query.limit(page, limit);
        List<LitemallAdmin> list = query.select();
		return list;
		/*
		 * LitemallAdmin admin = new LitemallAdmin(); admin.setUsername(username);
		 * return adminDao.template(admin);
		 * 
		 * 
		 * LitemallAdminExample example = new LitemallAdminExample();
		 * LitemallAdminExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(username)) { criteria.andUsernameLike("%" + username
		 * + "%"); } criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, limit); return
		 * adminMapper.selectByExampleSelective(example, result);
		 */
    }

    public int updateById(LitemallAdmin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        return adminDao.updateTemplateById(admin);
		/* return adminMapper.updateByPrimaryKeySelective(admin); */
    }

    public void deleteById(Integer id) {
    	LitemallAdmin admin = new LitemallAdmin();
    	admin.setId(id);
    	admin.setDeleted(true);
    	adminDao.updateTemplateById(admin);
		/* adminMapper.logicalDeleteByPrimaryKey(id); */
    }

    public void add(LitemallAdmin admin) {
        admin.setAddTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        adminDao.insertTemplate(admin,true);
		/* adminMapper.insertSelective(admin); */
    }

    public LitemallAdmin findById(Integer id) {
    	return adminDao.single(id);
		/* return adminMapper.selectByPrimaryKeySelective(id, result); */
    }

    public List<LitemallAdmin> all() {
    	LitemallAdmin admin  = new LitemallAdmin();
    	admin.setDeleted(false);
    	return adminDao.template(admin);
    	
		/*
		 * LitemallAdminExample example = new LitemallAdminExample();
		 * example.or().andDeletedEqualTo(false); return
		 * adminMapper.selectByExample(example);
		 */
    }
}
